﻿using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using DevExpress.XtraReports.UI;
using System.Configuration;
using IB_report_stand.Objects;
using System.Collections.Generic;
using System.Linq;
using IB_report_stand.DAL;

namespace IB_report_stand
{
    public partial class rpt_stock : DevExpress.XtraReports.UI.XtraReport
    {
        private DateTime _startDate;
        private DateTime _endDate;

        public rpt_stock()
        {
            InitializeComponent();
            initializeReport();
        }

        public rpt_stock(DateTime startDate, DateTime endTime)
        {
            InitializeComponent();
            

            this._startDate = startDate;
            this._endDate = endTime;

            lblStartDate.Text = _startDate.ToShortDateString();
            lblEndDate.Text = _endDate.ToShortDateString();

            lblCompanyName.Text = ConfigurationManager.AppSettings["companyName"];
            lblCompanyAddress.Text = ConfigurationManager.AppSettings["companyAddress"];

            initializeReport();
        }

        public void initializeReport()
        {
            DataObjectsDataContext dataContext = new DataObjectsDataContext();

            var dataReport = from stockRpt in dataContext.func_report_stock_by_item(_startDate, _endDate)
                             select stockRpt;

            List<ReportStock> lstSource = new List<ReportStock>();
            foreach (func_report_stock_by_itemResult item in dataReport)
            {
                ReportStock obj = new ReportStock() { 
                    ItemCode = item.itemcode, 
                    ItemName = item.item_descriptions, 
                    ItemAssortment = item.group_descriptions, 
                    Unit = item.unit, 
                    GLAccount = item.LGAccountDist, 
                    Warehouse_location = item.warehouse_location 
                };

                if (item.open_cost.HasValue)
                {
                    obj.OpenCost = item.open_cost.Value;
                }

                if (item.open_quantity.HasValue)
                {
                    obj.OpenQuantity = item.open_quantity.Value;
                }

                if (item.inperiod_cost_in.HasValue)
                {
                    obj.InPeriodCostIn = item.inperiod_cost_in.Value;
                }

                if (item.inperiod_quanlity_in.HasValue)
                {
                    obj.InPeriodQuantityIn = item.inperiod_quanlity_in.Value;
                }

                if (item.inperiod_cost_out.HasValue)
                {
                    obj.InPeriodCostOut = item.inperiod_cost_out.Value;
                }

                if (item.inperiod_quanlity_in.HasValue)
                {
                    obj.InPeriodQuantityOut = item.inperiod_quanlity_out.Value;
                }

                if (item.close_price.HasValue)
                {
                    obj.EndCost = item.close_price.Value;
                }

                if (item.close_quantity.HasValue)
                {
                    obj.EndQuantity = item.close_quantity.Value;
                }

                lstSource.Add(obj);
            }
            this.bindingSource.DataSource = lstSource;
        }
    }
}
